<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>3. Python 简介 &mdash; Python tutorial 3.4 documentation</title>
    
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '3.4',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Python tutorial 3.4 documentation" href="index.html" />
    <link rel="next" title="4. 深入 Python 流程控制" href="controlflow.html" />
    <link rel="prev" title="2. 使用 Python 解释器" href="interpreter.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="controlflow.html" title="4. 深入 Python 流程控制"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="interpreter.html" title="2. 使用 Python 解释器"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Python tutorial 3.4 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="python">
<span id="tut-informal"></span><h1>3. Python 简介<a class="headerlink" href="#python" title="Permalink to this headline">¶</a></h1>
<p>下面的例子中，输入和输出分别由大于号和句号提示符（ <tt class="docutils literal"><span class="pre">&gt;&gt;&gt;</span></tt> 和 <tt class="docutils literal"><span class="pre">...</span></tt> ）标注：如果想重现这些例子，就要在解释器的提示符后，输入（提示符后面的）那些不包含提示符的代码行。需要注意的是在练习中遇到的从属提示符表示你需要在最后多输入一个空行，解释器才能知道这是一个多行命令的结束。</p>
<p>本手册中的很多示例——包括那些带有交互提示符的——都含有注释。Python 中的注释以 <tt class="docutils literal"><span class="pre">#</span></tt> 字符起始，直至实际的行尾（译注——这里原作者用了 physical line 以表示实际的换行而非编辑器的自动换行）。注释可以从行首开始，也可以在空白或代码之后，但是不出现在字符串中。文本字符串中的 <tt class="docutils literal"><span class="pre">#</span></tt> 字符仅仅表示 <tt class="docutils literal"><span class="pre">#</span></tt> 。代码中的注释不会被 Python 解释，录入示例的时候可以忽略它们。</p>
<p>如下示例:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># this is the first comment</span>
<span class="n">SPAM</span> <span class="o">=</span> <span class="mi">1</span>                 <span class="c"># and this is the second comment</span>
                         <span class="c"># ... and now a third!</span>
<span class="n">STRING</span> <span class="o">=</span> <span class="s">&quot;# This is not a comment.&quot;</span>
</pre></div>
</div>
<div class="section" id="tut-calculator">
<span id="id1"></span><h2>3.1. 将 Python 当做计算器<a class="headerlink" href="#tut-calculator" title="Permalink to this headline">¶</a></h2>
<p>我们来尝试一些简单的 Python 命令。启动解释器然后等待主提示符 <tt class="docutils literal"><span class="pre">&gt;&gt;&gt;</span></tt> 出现。（不需要很久。）</p>
<div class="section" id="tut-numbers">
<span id="id2"></span><h3>3.1.1. 数字<a class="headerlink" href="#tut-numbers" title="Permalink to this headline">¶</a></h3>
<p>解释器的表示就像一个简单的计算器：可以向其录入一些表达式，它会给出返回值。表达式语法很直白：运算符 <tt class="docutils literal"><span class="pre">+</span></tt> ， <tt class="docutils literal"><span class="pre">-</span></tt> ， <tt class="docutils literal"><span class="pre">*</span></tt> 和 <tt class="docutils literal"><span class="pre">/</span></tt> 与其它语言一样（例如： Pascal 或 C）；括号用于分组。例如:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="mi">2</span><span class="o">+</span><span class="mi">2</span>
<span class="go">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># This is a comment</span>
<span class="gp">... </span><span class="mi">2</span><span class="o">+</span><span class="mi">2</span>
<span class="go">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">2</span><span class="o">+</span><span class="mi">2</span>  <span class="c"># and a comment on the same line as code</span>
<span class="go">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="mi">50</span><span class="o">-</span><span class="mi">5</span><span class="o">*</span><span class="mi">6</span><span class="p">)</span><span class="o">/</span><span class="mi">4</span>
<span class="go">5.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">8</span><span class="o">/</span><span class="mi">5</span> <span class="c"># Fractions aren&#39;t lost when dividing integers</span>
<span class="go">1.6</span>
</pre></div>
</div>
<p>注意：有时你可能会得到不同的结果；浮点数在不同机器上的运算结果可能是不同的。 后面我们将对控制浮点数输出的显示结果做更多说明；这里我们看到的仅是有效的显示，并非我们能得到的可读性更好的结果。</p>
<p>对整数做除法运算并想去除小数部分取得整数结果时，可以使用另外一个运算符， <tt class="docutils literal"><span class="pre">//</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Integer division returns the floor:</span>
<span class="gp">... </span><span class="mi">7</span><span class="o">//</span><span class="mi">3</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">7</span><span class="o">//-</span><span class="mi">3</span>
<span class="go">-3</span>
</pre></div>
</div>
<p>等号（ <tt class="docutils literal"><span class="pre">'='</span></tt> ）用于给变量赋值:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">width</span> <span class="o">=</span> <span class="mi">20</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">height</span> <span class="o">=</span> <span class="mi">5</span><span class="o">*</span><span class="mi">9</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">width</span> <span class="o">*</span> <span class="n">height</span>
<span class="go">900</span>
</pre></div>
</div>
<p>一个值可以同时赋给几个变量:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">y</span> <span class="o">=</span> <span class="n">z</span> <span class="o">=</span> <span class="mi">0</span>  <span class="c"># Zero x, y and z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span>
<span class="go">0</span>
</pre></div>
</div>
<p>变量在使用前必须“定义”（赋值），否则会出错:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># try to access an undefined variable</span>
<span class="gp">... </span><span class="n">n</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">NameError</span>: <span class="n">name &#39;n&#39; is not defined</span>
</pre></div>
</div>
<p>浮点数有完整的支持；与整型混合计算时会自动转为浮点数:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="mi">3</span> <span class="o">*</span> <span class="mf">3.75</span> <span class="o">/</span> <span class="mf">1.5</span>
<span class="go">7.5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mf">7.0</span> <span class="o">/</span> <span class="mi">2</span>
<span class="go">3.5</span>
</pre></div>
</div>
<p>复数也得到支持；带有后缀 <tt class="docutils literal"><span class="pre">j</span></tt> 或 <tt class="docutils literal"><span class="pre">J</span></tt> 就被视为虚数。带有非零实部的复数写为 <tt class="docutils literal"><span class="pre">(real+imagj)</span></tt> ，或者可以用 <tt class="docutils literal"><span class="pre">complex(real,</span> <span class="pre">imag)</span></tt> 函数创建。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="mi">1j</span> <span class="o">*</span> <span class="mi">1</span><span class="n">J</span>
<span class="go">(-1+0j)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">1j</span> <span class="o">*</span> <span class="nb">complex</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">(-1+0j)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">3</span><span class="o">+</span><span class="mi">1j</span><span class="o">*</span><span class="mi">3</span>
<span class="go">(3+3j)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="mi">3</span><span class="o">+</span><span class="mi">1j</span><span class="p">)</span><span class="o">*</span><span class="mi">3</span>
<span class="go">(9+3j)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="mi">2j</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="mi">1j</span><span class="p">)</span>
<span class="go">(1.5+0.5j)</span>
</pre></div>
</div>
<p>复数的实部和虚部总是记为两个浮点数。要从复数 z 中提取实部和虚部，使用 <tt class="docutils literal"><span class="pre">z.real</span></tt> 和 <tt class="docutils literal"><span class="pre">z.imag</span></tt> 。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">=</span><span class="mf">1.5</span><span class="o">+</span><span class="mf">0.5j</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">real</span>
<span class="go">1.5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">imag</span>
<span class="go">0.5</span>
</pre></div>
</div>
<p>浮点数和整数之间的转换函数（ <tt class="xref py py-func docutils literal"><span class="pre">float()</span></tt> 和 <tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt> 以及 <tt class="xref py py-func docutils literal"><span class="pre">long()</span></tt> ） 不能用于复数。没有什么正确方法可以把一个复数转成一个实数。函数 <tt class="docutils literal"><span class="pre">abs(z)</span></tt> 用于获取其模（浮点数）或 <tt class="docutils literal"><span class="pre">z.real</span></tt>  获取其实部:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">=</span><span class="mf">3.0</span><span class="o">+</span><span class="mf">4.0j</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">?</span>
<span class="gr">TypeError</span>: <span class="n">can&#39;t convert complex to float; use abs(z)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">real</span>
<span class="go">3.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">imag</span>
<span class="go">4.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">abs</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>  <span class="c"># sqrt(a.real**2 + a.imag**2)</span>
<span class="go">5.0</span>
</pre></div>
</div>
<p>交互模式中，最近一个表达式的值赋给变量 <tt class="docutils literal"><span class="pre">_</span></tt> 。这样我们就可以把它当作一个桌面计算器，很方便的用于连续计算，例如:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">tax</span> <span class="o">=</span> <span class="mf">12.5</span> <span class="o">/</span> <span class="mi">100</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">price</span> <span class="o">=</span> <span class="mf">100.50</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">price</span> <span class="o">*</span> <span class="n">tax</span>
<span class="go">12.5625</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">price</span> <span class="o">+</span> <span class="n">_</span>
<span class="go">113.0625</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">round</span><span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">113.06</span>
</pre></div>
</div>
<p>此变量对于用户是只读的。不要尝试给它赋值 —— 你只会创建一个独立的同名局部变量，它屏蔽了系统内置变量的魔术效果.</p>
</div>
<div class="section" id="tut-strings">
<span id="id3"></span><h3>3.1.2. 字符串<a class="headerlink" href="#tut-strings" title="Permalink to this headline">¶</a></h3>
<p>相比数值，Python 也提供了可以通过几种不同方式传递的字符串。它们可以用单引号或双引号标识:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;spam eggs&#39;</span>
<span class="go">&#39;spam eggs&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;doesn</span><span class="se">\&#39;</span><span class="s">t&#39;</span>
<span class="go">&quot;doesn&#39;t&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&quot;doesn&#39;t&quot;</span>
<span class="go">&quot;doesn&#39;t&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;&quot;Yes,&quot; he said.&#39;</span>
<span class="go">&#39;&quot;Yes,&quot; he said.&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&quot;</span><span class="se">\&quot;</span><span class="s">Yes,</span><span class="se">\&quot;</span><span class="s"> he said.&quot;</span>
<span class="go">&#39;&quot;Yes,&quot; he said.&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;&quot;Isn</span><span class="se">\&#39;</span><span class="s">t,&quot; she said.&#39;</span>
<span class="go">&#39;&quot;Isn\&#39;t,&quot; she said.&#39;</span>
</pre></div>
</div>
<p>Python 解释器按照字符串被输入的方式打印字符串结果：为了显示准确的值，字符串包含在成对的引号中，引号和其他特殊字符要用反斜线（ \ ）转译。 如果字符串只包含单引号（ &#8216; ）而没有双引号（ &#8221; ）就可以用双引号（ &#8221; ）包围，反之用单引号（ &#8216; ）包围。 再强调一下， <tt class="xref std std-keyword docutils literal"><span class="pre">print</span></tt> 函数可以生成可读性更好的输出。</p>
<p>字符串文本有几种方法分行。可以使用反斜杠为行结尾的连续字符串，它表示下一行在逻辑上是本行的后续内容:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">hello</span> <span class="o">=</span> <span class="s">&quot;This is a rather long string containing</span><span class="se">\n\</span>
<span class="s">several lines of text just as you would do in C.</span><span class="se">\n\</span>
<span class="s">    Note that whitespace at the beginning of the line is</span><span class="se">\</span>
<span class="s"> significant.&quot;</span>

<span class="k">print</span><span class="p">(</span><span class="n">hello</span><span class="p">)</span>
</pre></div>
</div>
<p>需要注意地是，还是需要在字符串中写入 n ——结尾的反斜杠会被忽略。前例会打印为如下形式:</p>
<div class="highlight-text"><div class="highlight"><pre>This is a rather long string containing
several lines of text just as you would do in C.
    Note that whitespace at the beginning of the line is significant.
</pre></div>
</div>
<p>另外，字符串可以标识在一对儿三引号中： <tt class="docutils literal"><span class="pre">`&quot;&quot;&quot;`</span></tt> 或 <tt class="docutils literal"><span class="pre">`'''`</span></tt> 。三引号中，不需要行属转义，它们已经包含在字符串中。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">print</span><span class="p">(</span><span class="s">&quot;&quot;&quot;</span><span class="se">\</span>
<span class="s">Usage: thingy [OPTIONS]</span>
<span class="s">     -h                        Display this usage message</span>
<span class="s">     -H hostname               Hostname to connect to</span>
<span class="s">&quot;&quot;&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>得到如下输出:</p>
<div class="highlight-text"><div class="highlight"><pre>Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
</pre></div>
</div>
<p>如果我们生成一个“原始”字符串， <tt class="docutils literal"><span class="pre">\n</span></tt> 序列不会被转义，而且行尾的反斜杠，源码中的换行符，都成为字符串中的一部分数据，因此下例:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">hello</span> <span class="o">=</span> <span class="s">r&quot;This is a rather long string containing\n</span><span class="se">\</span>
<span class="s">several lines of text much as you would do in C.&quot;</span>

<span class="k">print</span><span class="p">(</span><span class="n">hello</span><span class="p">)</span>
</pre></div>
</div>
<p>会打印:</p>
<div class="highlight-text"><div class="highlight"><pre>This is a rather long string containing\n\
several lines of text much as you would do in C.
</pre></div>
</div>
<p>字符串可以由 <tt class="docutils literal"><span class="pre">+</span></tt> 操作符连接（粘到一起），可以由 <tt class="docutils literal"><span class="pre">*</span></tt> 重复:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span> <span class="o">=</span> <span class="s">&#39;Help&#39;</span> <span class="o">+</span> <span class="s">&#39;A&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span>
<span class="go">&#39;HelpA&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;&lt;&#39;</span> <span class="o">+</span> <span class="n">word</span><span class="o">*</span><span class="mi">5</span> <span class="o">+</span> <span class="s">&#39;&gt;&#39;</span>
<span class="go">&#39;&lt;HelpAHelpAHelpAHelpAHelpA&gt;&#39;</span>
</pre></div>
</div>
<p>相邻的两个字符串文本自动连接在一起，前面那行代码也可以写为 <tt class="docutils literal"><span class="pre">word</span> <span class="pre">='Help'</span> <span class="pre">'A'</span></tt> ;它只用于两个字符串文本，不能用于字符串表达式:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;str&#39;</span> <span class="s">&#39;ing&#39;</span>                   <span class="c">#  &lt;-  This is ok</span>
<span class="go">&#39;string&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;str&#39;</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">+</span> <span class="s">&#39;ing&#39;</span>   <span class="c">#  &lt;-  This is ok</span>
<span class="go">&#39;string&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;str&#39;</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="s">&#39;ing&#39;</span>     <span class="c">#  &lt;-  This is invalid</span>
<span class="go">  File &quot;&lt;stdin&gt;&quot;, line 1, in ?</span>
<span class="go">    &#39;str&#39;.strip() &#39;ing&#39;</span>
<span class="go">                      ^</span>
<span class="go">SyntaxError: invalid syntax</span>
</pre></div>
</div>
<p>字符串也可以被截取（检索）。类似于 C ，字符串的第一个字符索引为 0 。没有独立的字符类型，字符就是长度为 1 的字符串。类似 Icon ，可以用 <em>切片标注</em> 法截取字符串：由两个索引分割的复本。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span>
<span class="go">&#39;A&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span>
<span class="go">&#39;He&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span>
<span class="go">&#39;lp&#39;</span>
</pre></div>
</div>
<p>索引切片可以有默认值，切片时，忽略第一个索引的话，默认为0，忽略第二个索引，默认为字符串的长度。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span>    <span class="c"># The first two characters</span>
<span class="go">&#39;He&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:]</span>    <span class="c"># Everything except the first two characters</span>
<span class="go">&#39;lpA&#39;</span>
</pre></div>
</div>
<p>不同于 C 字符串，Python 字符串不可变。向字符串文本的某一个索引赋值会引发错误:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;x&#39;</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">?</span>
<span class="gr">TypeError</span>: <span class="n">&#39;str&#39; object does not support item assignment</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[:</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;Splat&#39;</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">?</span>
<span class="gr">TypeError</span>: <span class="n">&#39;str&#39; object does not support slice assignment</span>
</pre></div>
</div>
<p>不过，组合文本内容生成一个新文本简单而高效:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;x&#39;</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
<span class="go">&#39;xelpA&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;Splat&#39;</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span>
<span class="go">&#39;SplatA&#39;</span>
</pre></div>
</div>
<p>切片操作有个有用的不变性： <tt class="docutils literal"><span class="pre">s[:i]</span> <span class="pre">+</span> <span class="pre">s[i:]</span></tt> 等于 <tt class="docutils literal"><span class="pre">s</span></tt>。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:]</span>
<span class="go">&#39;HelpA&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">3</span><span class="p">:]</span>
<span class="go">&#39;HelpA&#39;</span>
</pre></div>
</div>
<p>Python 能够优雅地处理那些没有意义的切片索引：一个过大的索引值（即下标值大于字符串实际长度）将被字符串实际长度所代替，当上边界比下边界大时（即切片左值大于右值）就返回空字符串。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">100</span><span class="p">]</span>
<span class="go">&#39;elpA&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">10</span><span class="p">:]</span>
<span class="go">&#39;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">1</span><span class="p">]</span>
<span class="go">&#39;&#39;</span>
</pre></div>
</div>
<p>索引也可以是负数，这将导致从右边开始计算。 例如:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>     <span class="c"># The last character</span>
<span class="go">&#39;A&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>     <span class="c"># The last-but-one character</span>
<span class="go">&#39;p&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">:]</span>    <span class="c"># The last two characters</span>
<span class="go">&#39;pA&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[:</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>    <span class="c"># Everything except the last two characters</span>
<span class="go">&#39;Hel&#39;</span>
</pre></div>
</div>
<p>请注意 -0 实际上就是 0 ，所以它不会导致从右边开始计算！</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">0</span><span class="p">]</span>     <span class="c"># (since -0 equals 0)</span>
<span class="go">&#39;H&#39;</span>
</pre></div>
</div>
<p>负索引切片越界会被截断，不要尝试将它用于单元素（非切片）检索:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">100</span><span class="p">:]</span>
<span class="go">&#39;HelpA&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">10</span><span class="p">]</span>    <span class="c"># error</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">?</span>
<span class="gr">IndexError</span>: <span class="n">string index out of range</span>
</pre></div>
</div>
<p>有个办法可以很容易地记住切片的工作方式：切片时的索引是在两个字符 <em>之间</em> 。左边第一个字符的索引为0，，而长度为 <em>n</em>  的字符串其最后一个字符的右界索引为 <em>n</em> 。例如:</p>
<div class="highlight-python"><div class="highlight"><pre> +---+---+---+---+---+
 | H | e | l | p | A |
 +---+---+---+---+---+
 0   1   2   3   4   5
-5  -4  -3  -2  -1
</pre></div>
</div>
<p>文本中的第一行数字给出字符串中的索引点 0...5 。第二行给出相应的负索引。切片是从 <em>i</em> 到 <em>j</em> 两个数值标示的边界之间的所有字符。</p>
<p>对于非负索引，如果上下都在边界内，切片长度与索引不同。例如， <tt class="docutils literal"><span class="pre">word[1:3]</span></tt> 是 2 。</p>
<p>内置函数 <tt class="xref py py-func docutils literal"><span class="pre">len()</span></tt> 返回字符串长度:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="s">&#39;supercalifragilisticexpialidocious&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="go">34</span>
</pre></div>
</div>
</div>
<div class="section" id="unicode">
<span id="tut-unicodestrings"></span><h3>3.1.3. 关于 Unicode<a class="headerlink" href="#unicode" title="Permalink to this headline">¶</a></h3>
<p>从 Python 3.0 开始所有的字符串都支持 Unicode（参考 <a class="reference external" href="http://www.unicode.org">http://www.unicode.org</a> ）。</p>
<p>Unicode 的先进之处在于为每一种现代或古代使用的文字系统中出现的每一个字符都提供了统一的序列号。之前，文字系统中的字符只能有 256 种可能的顺序。通过代码页分界映射。文本绑定到映射文字系统的代码页。这在软件国际化的时候尤其麻烦 （通常写作 <tt class="docutils literal"><span class="pre">i18n</span></tt> —— <tt class="docutils literal"><span class="pre">'i'</span></tt> + 18 个字符 + <tt class="docutils literal"><span class="pre">'n'</span></tt> ）。Unicode 解决了为所有的文字系统设置一个独立代码页的难题。</p>
<p>如果想在字符串中包含特殊字符，你可以使用Python的 <em>Unicode_Escape</em> 编码方式。 下面的例子展示了如何这样做:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;Hello</span><span class="se">\u0020</span><span class="s">World !&#39;</span>
<span class="go">&#39;Hello World !&#39;</span>
</pre></div>
</div>
<p>转码序列 <tt class="docutils literal"><span class="pre">\u0020</span></tt> 表示在指定位置插入编码为 0x0020 的 Unicode 字符（空格）。</p>
<p>其他字符就像 Unicode 编码一样被直接解释为对应的编码值。 如果你有在许多西方国家使用的标准 Latin-1 编码的字符串，你会发现编码小于 256 的 Unicode 字符和在 Latin-1 编码中的一样。</p>
<p>除了这些标准编码，Python 还提供了一整套基于其他已知编码创建 Unicode 字符串的方法。</p>
<p>字符串对象提供了一个 <tt class="xref py py-func docutils literal"><span class="pre">encode()</span></tt> 方法用以将字符串转换成特定编码的字节序列，它接收一个小写的编码名称作为参数。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">&quot;Äpfel&quot;</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;utf-8&#39;</span><span class="p">)</span>
<span class="go">b&#39;\xc3\x84pfel&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="tut-lists">
<span id="id4"></span><h3>3.1.4. 列表<a class="headerlink" href="#tut-lists" title="Permalink to this headline">¶</a></h3>
<p>Python 有几个 <em>复合</em> 数据类型，用于分线其它的值。最通用的是 <em>list</em> (列表) ，它可以写作中括号之间的一列逗号分隔的值。列表的元素不必是同一类型。</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;spam&#39;</span><span class="p">,</span> <span class="s">&#39;eggs&#39;</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">1234</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[&#39;spam&#39;, &#39;eggs&#39;, 100, 1234]</span>
</pre></div>
</div>
<p>就像字符串索引，列表从 0 开始检索。列表可以被切片和连接:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">&#39;spam&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
<span class="go">1234</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>
<span class="go">100</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="go">[&#39;eggs&#39;, 100]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="s">&#39;bacon&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="mi">2</span><span class="p">]</span>
<span class="go">[&#39;spam&#39;, &#39;eggs&#39;, &#39;bacon&#39;, 4]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">3</span><span class="o">*</span><span class="n">a</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="s">&#39;Boo!&#39;</span><span class="p">]</span>
<span class="go">[&#39;spam&#39;, &#39;eggs&#39;, 100, &#39;spam&#39;, &#39;eggs&#39;, 100, &#39;spam&#39;, &#39;eggs&#39;, 100, &#39;Boo!&#39;]</span>
</pre></div>
</div>
<p>所有的切片操作都会返回新的列表，包含求得的元素。这意味着以下的切片操作返回列表 <em>a</em> 的一个浅拷贝的副本:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[:]</span>
<span class="go">[&#39;spam&#39;, &#39;eggs&#39;, 100, 1234]</span>
</pre></div>
</div>
<p>不像 <em>不可变的</em> 字符串，列表允许修改元素:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[&#39;spam&#39;, &#39;eggs&#39;, 100, 1234]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="mi">23</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[&#39;spam&#39;, &#39;eggs&#39;, 123, 1234]</span>
</pre></div>
</div>
<p>也可以对切片赋值，此操作可以改变列表的尺寸，或清空它:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Replace some items:</span>
<span class="gp">... </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[1, 12, 123, 1234]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># Remove some:</span>
<span class="gp">... </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[123, 1234]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># Insert some:</span>
<span class="gp">... </span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;bletch&#39;</span><span class="p">,</span> <span class="s">&#39;xyzzy&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[123, &#39;bletch&#39;, &#39;xyzzy&#39;, 1234]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># Insert (a copy of) itself at the beginning</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[:</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[123, &#39;bletch&#39;, &#39;xyzzy&#39;, 1234, 123, &#39;bletch&#39;, &#39;xyzzy&#39;, 1234]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># Clear the list: replace all items with an empty list</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[:]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">[]</span>
</pre></div>
</div>
<p>内置函数 <tt class="xref py py-func docutils literal"><span class="pre">len()</span></tt> 同样适用于列表:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">,</span> <span class="s">&#39;c&#39;</span><span class="p">,</span> <span class="s">&#39;d&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">4</span>
</pre></div>
</div>
<p>允许嵌套列表（创建一个包含其它列表的列表），例如:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">q</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="n">q</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="go">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="go">[2, 3]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
<span class="go">2</span>
</pre></div>
</div>
<p>你可以在列表末尾添加内容:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;xtra&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span>
<span class="go">[1, [2, 3, &#39;xtra&#39;], 4]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span>
<span class="go">[2, 3, &#39;xtra&#39;]</span>
</pre></div>
</div>
<p>注意最后一个例子中， <tt class="docutils literal"><span class="pre">p[1]</span></tt> 和 <tt class="docutils literal"><span class="pre">q</span></tt> 实际上指向同一个对象！ 我们会在后面的 <em>object semantics</em> 中继续讨论。</p>
</div>
</div>
<div class="section" id="tut-firststeps">
<span id="id5"></span><h2>3.2. 编程的第一步<a class="headerlink" href="#tut-firststeps" title="Permalink to this headline">¶</a></h2>
<p>当然，我们可以使用 Python 完成比二加二更复杂的任务。例如，我们可以写一个生成 <em>菲波那契</em> 子序列的程序，如下所示:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Fibonacci series:</span>
<span class="gp">... </span><span class="c"># the sum of two elements defines the next</span>
<span class="gp">... </span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">while</span> <span class="n">b</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="gp">...</span>
<span class="go">1</span>
<span class="go">1</span>
<span class="go">2</span>
<span class="go">3</span>
<span class="go">5</span>
<span class="go">8</span>
</pre></div>
</div>
<p>这个例子介绍了几个新功能。</p>
<ul>
<li><p class="first">第一行包括了一个 <em>多重赋值</em> ：变量 <tt class="docutils literal"><span class="pre">a</span></tt> 和 <tt class="docutils literal"><span class="pre">b</span></tt> 同时获得了新的值 0 和 1 最后一行又使用了一次。在这个演示中，变量赋值前，右边首先完成计算。右边的表达式从左到右计算。</p>
</li>
<li><p class="first">条件（这里是 <tt class="docutils literal"><span class="pre">b</span> <span class="pre">&lt;</span> <span class="pre">10</span></tt> ）为 true 时， <tt class="xref std std-keyword docutils literal"><span class="pre">while</span></tt> 循环执行。在 Python 中，类似于 C ，任何非零整数都是 true；0 是 false 条件也可以是字符串或列表，实际上可以是任何序列；所有长度不为零的是 true ，空序列是 false。示例中的测试是一个简单的比较。标准比较操作符与 C 相同： <tt class="docutils literal"><span class="pre">&lt;</span></tt> （小于）， <tt class="docutils literal"><span class="pre">&gt;</span></tt> （大于）， <tt class="docutils literal"><span class="pre">==</span></tt> （等于）， <tt class="docutils literal"><span class="pre">&lt;=</span></tt> （小于等于）， <tt class="docutils literal"><span class="pre">&gt;=</span></tt> （大于等于）和 <tt class="docutils literal"><span class="pre">!=</span></tt> （不等于）。</p>
</li>
<li><p class="first">循环 <em>体</em> 是 <em>缩进</em> 的：缩进是 Python 是 Python 组织語句的方法。 Python (还) 不提供集成的行编辑功能，所以你要为每一个缩进行输入 TAB 或空格。实践中建议你找个文本编辑来录入复杂的 Python 程序，大多数文本编辑器提供自动缩进。交互式录入复合语句时，必须在最后输入一个空行来标识结束（因为解释器没办法猜测你输入的哪一行是最后一行），需要 注意的是同一个语句块中的语句块必须缩进同样数量的空白。</p>
</li>
<li><p class="first">关键字 <tt class="xref std std-keyword docutils literal"><span class="pre">print</span></tt> 语句输出给定表达式的值。它控制多个表达式和字符串输出为你想要字符串（就像我们在前面计算器的例子中那样）。字符串打印时不用引号包围，每两个子项之间插入空间，所以你可以把格式弄得很漂亮，像这样:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="mi">256</span><span class="o">*</span><span class="mi">256</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="s">&#39;The value of i is&#39;</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
<span class="go">The value of i is 65536</span>
</pre></div>
</div>
<p>用一个逗号结尾就可以禁止输出换行:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">while</span> <span class="n">b</span> <span class="o">&lt;</span> <span class="mi">1000</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">print</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">&#39;,&#39;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="gp">...</span>
<span class="go">1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,</span>
</pre></div>
</div>
</li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">3. Python 简介</a><ul>
<li><a class="reference internal" href="#tut-calculator">3.1. 将 Python 当做计算器</a><ul>
<li><a class="reference internal" href="#tut-numbers">3.1.1. 数字</a></li>
<li><a class="reference internal" href="#tut-strings">3.1.2. 字符串</a></li>
<li><a class="reference internal" href="#unicode">3.1.3. 关于 Unicode</a></li>
<li><a class="reference internal" href="#tut-lists">3.1.4. 列表</a></li>
</ul>
</li>
<li><a class="reference internal" href="#tut-firststeps">3.2. 编程的第一步</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="interpreter.html"
                        title="previous chapter">2. 使用 Python 解释器</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="controlflow.html"
                        title="next chapter">4. 深入 Python 流程控制</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/introduction.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="controlflow.html" title="4. 深入 Python 流程控制"
             >next</a> |</li>
        <li class="right" >
          <a href="interpreter.html" title="2. 使用 Python 解释器"
             >previous</a> |</li>
        <li><a href="index.html">Python tutorial 3.4 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2013, D.D.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.1.
    </div>
  </body>
</html>